package br.com.solutions.delivery.controller;

import br.com.solutions.delivery.dao.intf.IDao;
import br.com.solutions.delivery.entity.City;
import br.com.solutions.delivery.factory.dao.FactoryDAO;
import br.com.solutions.delivery.screen.CityScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CityScreenController{
  
    private IDao<City> dao;
    
    private ListIterator<City> iterator;
    
    private CityScreen form;
    
    private static CityScreenController instance;

    private class SaveActionListener implements ActionListener {
        
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                City city =  new City();
                city.setId(form.getTxtFieldid().getText());
                city.setName(form.getTxtFieldname().getText());
                dao.save(city);
            } catch (Exception ex) {
                Logger.getLogger(CityScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }    
   
    private class FirstActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                City city = dao.getAll().get(0);
                if (city != null){
                    form.getTxtFieldid().setText(city.getId());
                    form.getTxtFieldname().setText(city.getName());
                }
            } catch (Exception ex) {
                Logger.getLogger(CityScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    private class PriorActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(iterator.hasPrevious()){
                    City city = iterator.previous();
                    if (city != null){
                        form.getTxtFieldid().setText(city.getId().toString());
                        form.getTxtFieldname().setText(city.getName());
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(CityScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    private class NextActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(iterator.hasNext()){
                    City city = iterator.next();
                    if (city != null){
                        form.getTxtFieldid().setText(city.getId().toString());
                        form.getTxtFieldname().setText(city.getName());
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(CityScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    private class LastActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                City city = dao.getAll().get(dao.getAll().size() - 1);
                if (city != null){
                    form.getTxtFieldid().setText(city.getId());
                    form.getTxtFieldname().setText(city.getName());
                }
            } catch (Exception ex) {
                Logger.getLogger(CityScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
    private void addListeners() {
        this.form.getBtnSave().addActionListener(new SaveActionListener());
        this.form.getBtnPrior().addActionListener(new PriorActionListener());
        this.form.getBtnNext().addActionListener(new NextActionListener());
        this.form.getBtnFirst().addActionListener(new FirstActionListener());
        this.form.getBtnLast().addActionListener(new LastActionListener());
    }
    
    public void showScreen(){
        this.form.setVisible(true);
    }
        
    private CityScreenController() throws Exception{
        this.dao = FactoryDAO.getInstance().getDao(City.class);
        this.iterator = dao.getAll().listIterator();
        this.form = CityScreen.getInstance();
        addListeners();
        form.setVisible(true);
    }
    
    public static CityScreenController getInstance() throws Exception{
        if (instance == null){
            instance = new CityScreenController();
        }
        
        return instance;
    }
    


}
